package www.bplead.com.mydemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import www.bplead.com.controller.action.student.entity.Class;
import www.bplead.com.controller.action.student.entity.Course;
import www.bplead.com.controller.action.student.entity.Student;
import www.bplead.com.controller.action.student.event.StudentQueryResponse;
import www.bplead.com.controller.action.student.event.StudentUpdateRequest;
import www.bplead.com.controller.action.student.event.StudentUpdateResponse;
import www.bplead.com.mydemo.R;

/**
 * Created by wangyangzi on 2016/10/19.
 */
public class StudentInfoActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "StudentListActivity";

    TextView funTV;
    EditText nameET;
    EditText noET;
    TextView classTV;
    TextView courseTV;
    int classId;
    String className;
    int courseId;
    String courseName;
    View classView;
    View courseView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_info);

        funTV = (TextView) this.findViewById(R.id.fun_complete_tv);
        nameET = (EditText) this.findViewById(R.id.stu_info_name_et);
        noET = (EditText) this.findViewById(R.id.stu_info_no_et);
        classTV = (TextView) this.findViewById(R.id.stu_info_class_tv);
        courseTV = (TextView) this.findViewById(R.id.stu_info_course_tv);
        classView = this.findViewById(R.id.stu_class_layout);
        courseView = this.findViewById(R.id.stu_course_layout);

        funTV.setOnClickListener(this);
        classView.setOnClickListener(this);
        courseView.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == OPTION_REQUEST_CODE){
            int id = data.getIntExtra("id",-1);
            String name = data.getStringExtra("name");
            //选择的班级
            if(resultCode == 1){
                classId = id;
                className = name;
                classTV.setText(name + " >");
            }
            //选择的课程
            else if(resultCode == 2){
                courseId = id;
                courseName = name;
                courseTV.setText(name + " >");
            }
        }
    }

    int SAVE_RESULT_CODE = 1000;
    @Subscribe
    public void showStudentInfo(StudentUpdateResponse response){
        if(response == null){
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("student",response.student);
        this.setResult(SAVE_RESULT_CODE,intent);
        finish();
    }

    static final int OPTION_REQUEST_CODE = 2000;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fun_complete_tv:
                if(TextUtils.isEmpty(nameET.getText())){
                    Toast.makeText(this,"姓名不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(noET.getText())){
                    Toast.makeText(this,"学号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(className)){
                    Toast.makeText(this,"请选择班级",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(courseName)){
                    Toast.makeText(this,"请选择课程",Toast.LENGTH_SHORT).show();
                    return;
                }
                saveStudentRequest();
                break;
            case R.id.stu_class_layout:
                Intent intent2 = new Intent(this,StudentOptionActivity.class);
                intent2.putExtra("option_type","class");
                startActivityForResult(intent2,OPTION_REQUEST_CODE);
                break;
            case R.id.stu_course_layout:
                Intent intent3 = new Intent(this,StudentOptionActivity.class);
                intent3.putExtra("option_type","course");
                startActivityForResult(intent3,OPTION_REQUEST_CODE);
                break;
        }
    }

    private void saveStudentRequest() {
        Class cla = new Class();
        cla.setId(classId);
        cla.setName(className);
        Course cou = new Course();
        cou.setId(courseId);
        cou.setName(courseName);
        List<Course> courseList = new ArrayList<>();
        courseList.add(cou);

        Student stu = new Student();
        stu.setName(nameET.getText().toString());
        stu.setNo(noET.getText().toString());
        stu.set_class(cla);
        stu.setCourseList(courseList);

        StudentUpdateRequest request = new StudentUpdateRequest();
        request.student = stu;

        EventBus.getDefault().post(request);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            this.overridePendingTransition(R.anim.activity_hide,R.anim.activity_translate_out);
        }
        return super.onKeyDown(keyCode, event);
    }

}
